﻿using Dapper;
using Gx.Common.Core;
using Gx.Dal.Shared;
using Gx.Model.Entity;
using Gx.Model.Enums;
using Gx.Model.ViewModel;
using System.Collections.Generic;
using System.Linq;

namespace Gx.Dal.Manage
{
    public class LKCustomerDal:BaseDal<LKCustomer>
    {
        //public bool AddCustomer(LKCustomer customer)
        //{
        //    using (var conn = new DbContext())
        //    {
        //        return conn.Connection.Insert(customer) > 0;
        //    }
        //}

        //public bool UpdateCustomer(CustomerEditDto customer)
        //{
        //    using (var conn = new DbContext())
        //    {
        //        return conn.Connection.Update(customer);
        //    }
        //}

        //public List<LKCustomer> GetCustomerList()
        //{
        //    using (var conn = new DbContext())
        //    {
        //        return conn.Connection.GetAll<LKCustomer>().ToList();
        //    }
        //}

        /// <summary>
        /// 获取最大客户编号
        /// </summary>
        /// <returns></returns>
        public string GetMaxCustomerCode()
        {
            using (var conn = new DbContext())
            {
                return conn.Connection.QueryFirst<string>("SELECT MAX(CustomerCode) FROM LK_Customer");
            }
        }

        public PagedList<LKCustomer> GetCustomerList(CustomerInput input)
        {
            string sql = "SELECT * FROM LK_Customer WHERE IsDeleted=0 @WHERE";
            List<string> conditions = new List<string>();
            if (!string.IsNullOrWhiteSpace(input.CustomerName))
            {
                input.CustomerName = $"%{input.CustomerName}%";
                conditions.Add("CustomerName LIKE @CustomerName");
            }
            if (!string.IsNullOrWhiteSpace(input.CustomerCode))
            {
                input.CustomerCode = $"%{input.CustomerCode}%";
                conditions.Add("CustomerCode LIKE @CustomerCode");
            }
            if (input.CustomerLevelId.HasValue) 
            {
                conditions.Add("CustomerLevelId = @CustomerLevelId");
            }
            if (!string.IsNullOrWhiteSpace(input.Region)) 
            {
                conditions.Add("Region = @Region");
            }
            if (input.IsValid.HasValue)
            {
                conditions.Add("IsValid = @IsValid");
            }

            if (!string.IsNullOrWhiteSpace(input.Customer)) 
            {
                input.Customer = $"%{input.Customer}%";
                conditions.Add("(CustomerName LIKE @Customer OR CustomerCode LIKE @Customer)");
            }

            if (UserContext.RoleId != (int)Roles.管理员)
            {
                conditions.Add($"CompanyId={UserContext.CompanyId}");
            }

            string condition = string.Empty;
            if (conditions.Any())
            {
                condition = $" AND {string.Join(" AND ", conditions)}";
            }
            sql = !string.IsNullOrWhiteSpace(condition) ? sql.Replace("@WHERE", condition) : sql.Replace("@WHERE", "");

            using (var conn = new DbContext())
            {
                return conn.Connection.GetPagedList<LKCustomer>(sql, "CreateTime Desc", input);
            }
        }
    }
}
